package com.jstyle.jclife.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.jclife.BuildConfig;
import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.ContactAdapter;
import com.jstyle.jclife.adapter.RecyclerViewBaseAdapter;
import com.jstyle.jclife.app.MyApplication;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.adapter.UserInfoAdapter;
import com.jstyle.jclife.model.Device1810;
import com.jstyle.jclife.model.JstyleDevice;
import com.jstyle.jclife.model.SosContact;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.SchedulersTransformer;
import com.jstyle.jclife.utils.ScreenUtils;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class UserInfoActivity extends BaseActivity implements RecyclerViewBaseAdapter.OnItemClickListener, ContactAdapter.onItemClickListener {

    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.RecyclerView_userinfo)
    RecyclerView RecyclerViewUserinfo;
    @BindArray(R.array.userinfo_title)
    String[] arraySetting;
    @BindView(R.id.bt_contact)
    Button btContact;
    @BindView(R.id.RecyclerView_contact)
    RecyclerView RecyclerViewContact;
    private UserInfoAdapter userInfoAdapter;
    private String uid;
    private static final int requested_edit=1223;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        init();
    }
    ContactAdapter contactAdapter;
    private void init() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
         uid= NetWorkUtil.getUserId();
        UserInfo userInfo = UserInfoDaoManager.getUserByUid(uid);
        userInfoAdapter = new UserInfoAdapter(userInfo, this);
        userInfoAdapter.setTitles(arraySetting);
        userInfoAdapter.setOnItemClickListener(this);
        RecyclerViewUserinfo.setAdapter(userInfoAdapter);
        RecyclerViewUserinfo.setLayoutManager(linearLayoutManager);
        ScreenUtils.setTitleTypeface(btGoalBack);
        JstyleDevice jstyleDevice=MyApplication.getJstyleDevice();
        if (jstyleDevice instanceof Device1810) {
            RecyclerViewContact.setVisibility(View.GONE);
            btContact.setVisibility(View.GONE);
        }else{
            LinearLayoutManager linearLayoutManagerContact = new LinearLayoutManager(this);
            linearLayoutManagerContact.setOrientation(LinearLayoutManager.VERTICAL);
            RecyclerViewContact.setLayoutManager(linearLayoutManagerContact);
            getAllContact();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==requested_edit&&resultCode==RESULT_OK) getAllContact();
    }

    private void getAllContact(){
//        if(BuildConfig.APPLICATION_ID.equals("com.jstyle.fomofit"))return;
        NetWorkUtil.getInstance().getJstyleApi().getAllContact(uid).compose(SchedulersTransformer.<SosContact>io2MainObservable()).subscribe(new Observer<SosContact>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SosContact sosContact) {
                List<SosContact.DataBean>contactList=sosContact.getData();
                contactAdapter=new ContactAdapter(contactList);
                contactAdapter.setOnContactClickListener(UserInfoActivity.this);
                RecyclerViewContact.setAdapter(contactAdapter);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        UserInfo userInfo = UserInfoDaoManager.getUserByUid(NetWorkUtil.getUserId());
        userInfoAdapter.setUserInfo(userInfo);
    }

    @OnClick({R.id.iv_back,R.id.bt_contact})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_contact:
                showContact();
                break;
        }

    }
    boolean isShowContact=true;
    private void showContact() {
        isShowContact=!isShowContact;
        RecyclerViewContact.setVisibility(isShowContact?View.VISIBLE:View.GONE);
        Drawable drawable=getResources().getDrawable(isShowContact?R.drawable.down_contact:R.drawable.up_contact);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        btContact.setCompoundDrawables(null,null,drawable,null);
    }

    @Override
    public void onItemClick(int position) {
        if(position==7){
            Intent intent = new Intent(this, HeartTipsActivity.class);
            intent.putExtra(HeartTipsActivity.TipsType, HeartTipsActivity.TipsType_BMI);
            startActivity(intent);
            return;
        }
        if (position == 3) return;
        if (position > 3) position = position - 1;
        Intent intent = new Intent(this, UserInfoSettingActivity.class);
        intent.putExtra(UserInfoSettingActivity.userinfo_mode, position);
        startActivity(intent);
    }

    @Override
    public void onItemClick(SosContact.DataBean dataBean) {
        Intent intent=new Intent(this,AddContactActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable(AddContactActivity.CONTACT,dataBean);
        intent.putExtras(bundle);
        startActivityForResult(intent,requested_edit);
    }
}
